﻿#region
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HtmlAgilityPack;
using HtmlDocument = HtmlAgilityPack.HtmlDocument;

#endregion

namespace ComicsDownloader.Engine.Extractors
{
    public class StooExtractor : Extractor
    {
        internal StooExtractor(Cartoon cartoon) : base(cartoon)
        {
        }

        public override List<Picture> ExtractCartoons(int pageNo)
        {
            List<Picture> pictures = new List<Picture>();

            string html = Web.DownloadString(Cartoon.GetCartoonUrl(pageNo));

            HtmlDocument htmlDocument = new HtmlDocument();
            htmlDocument.LoadHtml(html);
            var nodes = htmlDocument.DocumentNode.SelectNodes("//table/tr/td/a/span");
            foreach (HtmlNode node in nodes)
            {
                var href = node.ParentNode.GetAttributeValue("href", string.Empty);

                var match = Regex.Match(href, @"\d{19}A");
                if (match.Value == string.Empty)
                    continue;

                string text = node.InnerText;

                pictures.Add(new Picture(text, match.Value, 1, Cartoon));
                pictures.Add(new Picture(text, match.Value, 2, Cartoon));
            }

            return pictures;


            /*
<table border="0" cellpadding="0" cellspacing="0">
    <tr>
            <td class="txt">
                    <a href="/cartoon/ctview.htm?sc2=ing&sc3=1&tpg=1&id=2014092709002571381A"><img src="http://cwcontent.asiae.co.kr/amgimagelink/98/2014092709002571381A_3.jpg" width="90" height="60" /></a>
                    <a href="/cartoon/ctview.htm?sc2=ing&sc3=1&tpg=1&id=2014092709002571381A"><span>4583화 바나나 한 송...</span></a>
            </td>
             */

            // /cartoon/ctview.htm?sc2=ing&sc3=1&tpg=1&id=2014092709002571381A
//            foreach (HtmlElement htmlElement in links)
//            {
//                Picture[] pictureArray = ExtractCartoonCore(htmlElement);
//                if (pictureArray == null)
//                    continue;
//
//                pictures.AddRange(pictureArray);
//            }

            return pictures.Distinct().OrderByDescending(x => x.Title).ToList();
        }

        private Picture[] ExtractCartoonCore(HtmlElement htmlElement)
        {
            if (htmlElement.OuterHtml == null)
                return null;

            var match = Regex.Match(htmlElement.OuterHtml, @"\d{19}A");

            if (match.Value == string.Empty)
                return null;

            if (htmlElement.InnerText == null)
                return null;

            Picture picture1 = new Picture(htmlElement.InnerText.Trim(), match.Value, 1, Cartoon);
            Picture picture2 = new Picture(htmlElement.InnerText.Trim(), match.Value, 2, Cartoon);

            return new[] {picture1, picture2};
        }
    }
}